package com.kura.uednews.fragment;

import java.util.ArrayList;

import com.kura.uedews.R;
import com.kura.uednews.db.newsDB;
import com.kura.uednews.model.Categories;
import com.kura.uednews.model.Contact;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class fragment_contact_list extends Fragment {
	
	ListView contactListView;
	MyContactAdapter adapter;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_contact_list, container,
				false);
		return view;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
		// get instance
		contactListView = (ListView) getActivity().findViewById(R.id.contactListView);
		adapter = new MyContactAdapter(getActivity());
		contactListView.setAdapter(adapter);
	}
}

class MyContactAdapter extends BaseAdapter {

	Context context;
	newsDB db;
	ArrayList<Contact> list;
	TextView fullName;
	TextView phoneContact;

	public MyContactAdapter() {

	}

	public MyContactAdapter(Context context) {
		this.context = context;
		db = new newsDB(context);
		list = db.getListContact();
	}

	public void changeData(int id, int parent) {

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.toArray().length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View row = null;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.custom_contact_row, parent, false);
		} else {
			row = convertView;
		}		

		fullName = (TextView) row.findViewById(R.id.fullName);
		fullName.setText(list.get(position).getName());
		phoneContact = (TextView) row.findViewById(R.id.phoneContact);
		phoneContact.setText(list.get(position).getPhone());
		
		return row;
	}

}
